package com.ywu.application.standard.role.service;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.result.CommonResult;
import com.ywu.application.standard.role.data.UserRoleAuthRepository;
import com.ywu.application.standard.role.data.UserRoleRepository;
import com.ywu.application.standard.role.dto.UserRoleAuthDto;
import com.ywu.application.standard.role.entity.UserRoleAuthEntity;
import com.ywu.application.standard.role.entity.UserRoleEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName UserRoleAuthService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/9 20:24
 * @Version 1.0
 **/
@Service
public class UserRoleAuthService {
    @Autowired
    private UserRoleAuthRepository userRoleAuthRepository;

    @Autowired
    private UserRoleRepository userRoleRepository;

    /**
     * @Author GroundDemo
     * @Description 查询用户关联的角色
     * @Date 20:25 2024/3/9
     * @Param [userRoleAuthDto]
     * @return com.ywu.common.result.CommonResult<java.lang.Object>
     **/
    public CommonResult<Object> queryUserRoleInfo(UserRoleAuthDto userRoleAuthDto) {
        List<UserRoleAuthEntity> entityList = userRoleAuthRepository.queryUserRoleInfo(userRoleAuthDto);
        if (CollectionUtils.isEmpty(entityList)) {
            return CommonResult.success(new ArrayList<>());
        }
        List<Integer> roleIds = entityList.stream().map(UserRoleAuthEntity::getRoleId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(roleIds)) {
            return CommonResult.success(new ArrayList<>());
        }
        List<UserRoleEntity> entities = userRoleRepository.queryBatchByRoleIds(roleIds);
        return CommonResult.success(entities);
    }

    /**
    * @Author GroundDemo
    * @Description 用户角色授权管理
    * @Date 20:53 2024/3/9
    * @Param [userRoleAuthDtos]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> userRoleAuthCreateInfo(List<UserRoleAuthDto> userRoleAuthDtos) {
        // 先删除，再插入
        List<Integer> userIds = userRoleAuthDtos.stream().map(UserRoleAuthDto::getUserId).collect(Collectors.toList());
        userRoleAuthRepository.deleteBatchByUserId(userIds);
        ArrayList<UserRoleAuthEntity> userRoleAuthEntities = new ArrayList<>();
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        userRoleAuthDtos.forEach(res -> {
            UserRoleAuthEntity entity = new UserRoleAuthEntity();
            BeanUtils.copyProperties(res, entity);
            entity.createInfo(currentContext.getYwuContextUser().getUserAccount());
            userRoleAuthEntities.add(entity);
        });
        if (CollectionUtils.isEmpty(userRoleAuthEntities)) {
            return CommonResult.success(null);
        }
        // 批量插入数据
        userRoleAuthRepository.batchInsertUserRoleInfo(userRoleAuthEntities);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除用户时删除授权信息
    * @Date 19:19 2025/3/26
    * @Param [userRoleAuthDtos]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> userRoleAuthDeleteInfo(List<UserRoleAuthDto> userRoleAuthDtos) {
        List<Integer> userIds = userRoleAuthDtos.stream().map(UserRoleAuthDto::getUserId).collect(Collectors.toList());
        userRoleAuthRepository.deleteBatchByUserId(userIds);
        return CommonResult.success(null);
    }
}
